package com.newtouch.avmarketing.mgr.modules.avmarkrting.service.impl;

import com.newtouch.avmarketing.mgr.modules.avmarkrting.entity.MeetIngInfo;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.model.MeetIngInfoCondition;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.repo.MeetIngRepo;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.service.MeetIngService;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class MeetIngServiceImpl implements MeetIngService {

    @Autowired
    private MeetIngRepo meetIngRepo;

    private Logger logger = LoggerFactory.getLogger(MeetIngServiceImpl.class);

    @Override
    public Page<List<MeetIngInfo>> findMeetingsByOprator(String oprator, int page, int num) {
        Pageable pageable = new PageRequest(page-1,num);
        Page<List<MeetIngInfo>> mrrtingList = meetIngRepo.findMeetIngListByOpNo(oprator,pageable);
        return mrrtingList;
    }

    @Override
    public Page<MeetIngInfo> findMeetIngInfo(MeetIngInfoCondition meetIngInfoCondition) {
        Specification<MeetIngInfo> spec = new Specification<MeetIngInfo>() {
            @SneakyThrows
            @Override
            public Predicate toPredicate(Root<MeetIngInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<>();
                //会议状态
                if (!StringUtils.isEmpty(meetIngInfoCondition.getMeetingStatus())) {
                    predicateList.add((root.get("meetingStatus").in(meetIngInfoCondition.getMeetingStatus())));
                }
                //起始日期
                if (!StringUtils.isEmpty(meetIngInfoCondition.getBegin()) && !meetIngInfoCondition.getBegin().equals("")) {
                    predicateList.add(cb.greaterThanOrEqualTo(root.get("endTime"), meetIngInfoCondition.getBegin()));
                }
                //结束日期
                if (!StringUtils.isEmpty(meetIngInfoCondition.getEnd()) && !meetIngInfoCondition.getEnd().equals("")) {
                    predicateList.add(cb.lessThanOrEqualTo(root.get("endTime"),meetIngInfoCondition.getEnd()));
                }
                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        //排序
        Sort sort = new Sort(Sort.Direction.DESC, "endTime");
        Pageable pageable = PageRequest.of(meetIngInfoCondition.getPageNumber()-1,meetIngInfoCondition.getPageSize(),sort);
        Page<MeetIngInfo> list = meetIngRepo.findAll(spec, pageable);
        return list;
    }
}
